package com.frinder.official.akka.persistence;

import akka.persistence.AbstractPersistentActor;
import akka.persistence.RecoveryCompleted;

/**
 * Created by frinder on 2017/10/10.
 */
public class MyPersistentActor5 extends AbstractPersistentActor {

    @Override
    public String persistenceId() {
        return "my-stable-persistence-id";
    }

    @Override
    public Receive createReceiveRecover() {
        return receiveBuilder().
                match(RecoveryCompleted.class, r -> {
                    // perform init after recovery, before any other messages
                    // ...
                }).
                match(String.class, this::handleEvent).build();
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder().
                match(
                        String.class,
                        s -> s.equals("cmd"),
                        s -> persist("evt", this::handleEvent)
                )
                .build();
    }

    private void handleEvent(String event) {
        // update state
        // ...
    }

}